﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace System
{
    public static class SystemExtensions
    {
        /// <summary>
        /// Extention of string to make a paskal or camel case string to a logical text. for example("ThisIsASample1Text => This Is A Sample 1 Text)
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToCaseWords(this string value)
        {
            Regex r = new Regex(@"(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
            return r.Replace(value, " ");
        }

        public static Byte[] ToByteArray(this string value, Encoding encoding)
        {
            Byte[] byteArray = encoding.GetBytes(value);
            return byteArray;
        }

        public static String ToString(this Byte[] value, Encoding encoding)
        {
            String constructedString = encoding.GetString(value);
            return (constructedString);
        }
    }
}
namespace System.Linq
{
    public static class LinkExtensions
    {
        public static int FirstIndexOf<T>(this IEnumerable<T> source, Predicate<T> predicate)
        {
            var index = 0;
            foreach (var item in source)
            {
                if (predicate(item))
                {
                    return index;
                }
                index++;
            }
            return -1;
        }
        public static IEnumerable<T> ForEach<T>(
            this IEnumerable<T> collection, Action<T> action)
        {
            foreach (var element in collection)
            {
                action(element);
            }
            return collection;
        }
        public static string AsString(this IEnumerable enumerable, string separator)
        {
            if (null == enumerable)
            {
                return string.Empty;
            }
            var enumerator = enumerable.GetEnumerator();
            try
            {
                return enumerator.AsString(separator);
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    ((IDisposable)enumerator).Dispose();
                }
            }
        }
        public static string AsString(this IEnumerator enumerator, string separator)
        {
            if (null == enumerator || !enumerator.MoveNext())
            {
                return string.Empty;
            }
            var result = new StringBuilder();
            result.Append(enumerator.Current);
            while (enumerator.MoveNext())
            {
                result.Append(separator);
                result.Append(enumerator.Current);
            }
            return result.ToString();
        }
        public static string AsString(this IEnumerable enumerable, string format, string separator)
        {
            if (null == enumerable)
            {
                return string.Empty;
            }
            var enumerator = enumerable.GetEnumerator();
            try
            {
                return enumerator.AsString(format, separator);
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    ((IDisposable)enumerator).Dispose();
                }
            }
        }
        public static string AsString(this IEnumerator enumerator, string format, string separator)
        {
            if (null == enumerator || !enumerator.MoveNext())
            {
                return string.Empty;
            }
            var result = new StringBuilder();
            result.AppendFormat(format, enumerator.Current);
            while (enumerator.MoveNext())
            {
                result.Append(separator);
                result.AppendFormat(format, enumerator.Current);
            }
            return result.ToString();
        }
        public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
        {
            return null == source || !source.Any();
        }
    }
}
